package com.tea.funct.utils.util;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.google.common.collect.Maps;
import com.tea.funct.utils.domain.RespBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 拉取其他服务器数据工具
 */
@Slf4j
@Component
public class PullDataUtil {

    /**
     * GET方式拉取数据
     *
     * @param url 拉取地址
     * @return
     */
    public Object getMethodPull(String url, String token, Class<? extends RespBean> clazz) {
        try {
            Map<String, String> map = this.getMD5RequestData();
            String timeStamp = map.get("timeStamp");
            String sign = map.get("sign");
            log.info("请求地址：{} \n请求方式：GET", url);
            String body = HttpUtil.createGet(url)
                    .header("X-Access-Token", token)
                    .header("X-Sign", sign)
                    .header("X-TIMESTAMP", timeStamp)
                    .execute()
                    .body();
            log.info("请求结果：{}", body);
            RespBean bean = JSONUtil.toBean(body, clazz);
            return bean;
        } catch (Exception e) {
            log.error("拉取数据出现异常：{}", e.getMessage());
            log.error(e.getMessage(), e);
        }
        return null;
    }

    private Map<String, String> getMD5RequestData() {
        HashMap<String, String> map = Maps.newHashMap();
        String salt = "dEB416XmG2ZOBQKnSzrVZMFERUCAwEAAQ";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String timeStamp = sdf.format(new Date()); // 当前时间信息
            String signStr = timeStamp + salt; // 加密前数据
            MessageDigest md = MessageDigest.getInstance("MD5");
            //通过md5计算摘要
            byte[] bytes = md.digest(signStr.getBytes(StandardCharsets.UTF_8));
            //md5值转成可读字符串
            StringBuilder signFlag = new StringBuilder(); // 加密后数据
            for (byte b : bytes) {
                // 字节转换为32位
                signFlag.append(String.format("%02x", b));
            }
            String sign = signFlag.toString().toUpperCase();
            log.info("当前时间：{}", timeStamp);
            log.info("sign数据：{}", sign);
            map.put("timeStamp", timeStamp);
            map.put("sign", sign);
        } catch (NoSuchAlgorithmException e) {
            log.error("MD5加密异常：{}", e.getMessage());
            log.error(e.getMessage(), e);
        }
        return map;
    }

    // 会员token
    // eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MTY4NjE3NDAsInVzZXJuYW1lIjoiMTU1MTc1Mjg3MjkifQ.UTt2E3-LYYssHUsGv9JXI113OAnr2Vl1ITNnMG3g6GA
    // 非会员权限的token
    // eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MTg3ODU0MTAsInVzZXJuYW1lIjoiMTM1MjYyMTczNTcifQ.hNo8zRoZ-DiU0WL5qLAj0gHQiI7qUZLPj_A8BOVhXlg
    public static void main(String[] args) {
        HashMap<String, String> map = Maps.newHashMap();
        String salt = "dEB416XmG2ZOBQKnSzrVZMFERUCAwEAAQ";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String timeStamp = sdf.format(new Date()); // 当前时间信息
            String signStr = timeStamp + salt; // 加密前数据
            MessageDigest md = MessageDigest.getInstance("MD5");
            //通过md5计算摘要
            byte[] bytes = md.digest(signStr.getBytes(StandardCharsets.UTF_8));
            //md5值转成可读字符串
            StringBuilder signFlag = new StringBuilder(); // 加密后数据
            for (byte b : bytes) {
                // 字节转换为32位
                signFlag.append(String.format("%02x", b));
            }
            String sign = signFlag.toString().toUpperCase();
            log.info("当前时间：{}", timeStamp);
            log.info("sign数据：{}", sign);
            map.put("timeStamp", timeStamp);
            map.put("sign", sign);
        } catch (NoSuchAlgorithmException e) {
            log.error("MD5加密异常：{}", e.getMessage());
            log.error(e.getMessage(), e);
        }

    }
}
